var fs = require("fs");
var charset = require('superagent-charset');
var request = require('superagent');
var cheerio = require('cheerio');
var $ = cheerio;
charset(request);

var ext = 'txt';
var filename = 'file_' + (new Date() - 0) + '.' + ext;

request
  .post('这里填写抓取的数据地址')
  .charset('gbk')
  .end(function (err, res) {
    var totalCount = $(res.res.text).find('#yxy [name=totalCount]').val();

    totalCount = parseInt(totalCount);
    var pageSize = Math.floor(totalCount / 12);
    pageSize = totalCount % 12 == 0 ? pageSize : pageSize + 1;
    var currentPage = 1;
    console.log(totalCount, pageSize);
    post(currentPage, totalCount, pageSize);
  });

function post(currentPage, totalCount, pageSize) {
  request
    .post('')
    .set({
      'Content-Type': 'application/x-www-form-urlencoded'
    })
    .send({
      currentPage: currentPage,
      totalCount: totalCount,
      isTable: true
    })
    .charset('gbk')
    .end(function (err, res) {
      var trs = cheerio(res.res.text).find('.cxjg tr');
      var tr_text = '';
      for (var i = 0, l = trs.length; i < l; i++) {
        var arr = [];
        for (var j = 0, ll = trs[i].children.length; j < ll; j++) {
          var td = trs[i].children[j];
          var td_text = $(td).text().trim();
          if (td_text.length > 0) {
            arr.push(ext == 'csv' ? '"' + td_text + '"' : td_text);
          }
        }
        tr_text += arr.join(',') + '\r\n';
      }
      console.log(tr_text + '\r\n');

      fs.appendFile(filename, tr_text + '\r\n', function (err) {
        if (err) throw err;
        console.log('write file success');
      });
      if (currentPage <= pageSize) {
        post(++currentPage, totalCount, pageSize);
      }
    });
}
